<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :colon="false" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="dateRange" label="申请时间" class="w-[320px] mr-2">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="zjm" class="w-[260px]">
          <a-input v-model:value="formState.zjm" placeholder="申请单号/病人姓名/助记码" />
        </a-form-item>
        <a-form-item class="w-[240px] ml-2">
          <a-radio-group v-model:value="formState.status">
            <a-radio value="">全部</a-radio>
            <a-radio value="1">未配血</a-radio>
            <a-radio value="3">已配血</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="类别" class="w-[280px]">
          <a-select v-model:value="formState.appPath" placeholder="请选择" allow-clear>
            <a-select-option value="0">门诊</a-select-option>
            <a-select-option value="1">住院</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <BaseTable :data-source="tableData" fill :loading="loading" :scroll="{ x: 2200 }" column-code="000635" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'appPath'">
            <span v-if="record?.appPath === 1" color="blue">住院</span>
            <span v-else color="red">门诊</span>
          </template>
          <template v-if="column.dataIndex === 'status'">
            <a-tag v-if="record?.status === 1" color="red">未配血</a-tag>
            <a-tag v-else color="blue">已配血</a-tag>
          </template>
          <template v-if="column.dataIndex === 'appLsh'">
            <PopoverTable :config="detailConfig" :params="{ appLsh: record.appLsh }" :width="1600">
              <template #bodyCell="{ column: popColumn, record: popRecord }">
                <template v-if="popColumn.dataIndex === 'isPass'">
                  {{ popRecord.isPass === '1' ? '是' : '否' }}
                </template>
              </template>
              {{ record.appLsh }}
            </PopoverTable>
          </template>
          <template v-if="column.dataIndex === 'admissionCode'">
            {{ record.patientInfo?.inpRecordCode }}
          </template>
          <template v-if="column.dataIndex === 'patientInfo'">
            <template v-if="record?.patientInfo">
              <span>{{ record.patientInfo?.brName }}</span>
              <span class="mx-2">{{ record.patientInfo?.brXb }}</span>
              <span>{{ record.patientInfo?.brAge }}</span>
              <span v-if="record?.appPath === 1" class="mx-2">{{ record.patientInfo?.bedCode }}</span>
              <span>{{ record.patientInfo?.deptName }}</span>
              <span v-if="record?.appPath === 1" class="mx-2">{{ record.patientInfo?.wardName }}</span>
            </template>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" :disabled="record.status === 3" @click="onDetail(record)">配血</a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { useRouter } from 'vue-router'
// import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
// import { post } from '@/utils/request'
import BaseTable from '@/components/BaseTable'

const router = useRouter()
const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().subtract(1, 'month').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  status: ''
})

const { loading, tableData, pagination, getData } = useTable({
  url: '/bloodm/BldTransfusionRecord/getBldTransfusionMatchList',
  immediate: true,
  formatParams: () => {
    return {
      ...formState.value,
      startDate: formState.value.dateRange?.[0],
      endDate: formState.value.dateRange?.[1]
    }
  }
})

const detailConfig = ref({
  url: '/bloodm/BldTransfusionRecord/getBldTransfusionRecordList',
  columnCode: '000638',
  rowKey: 'appLsh',
  formatResult: (data) => {
    return data.map((item, index) => ({
      ...item,
      xh: index + 1
    }))
  }
})

const onDetail = (record) => {
  router.push({
    path: '/blood-bank/with-blood/match-info',
    query: {
      appLsh: record.appLsh
    }
  })
}
</script>
